package just.stuff.morsels.util;

import java.util.Date;

public class DateIntervalFormat {

	private static final String[] INTERVALS = new String[] { "second", "minute", "hour", "day", "month", "year", "decade" };
	private static final int [] INTERVAL_DIVISORS = new int[] { 1, 60, 60, 24, 30, 365, 10 };
	
	public static String format( Date from, Date to ) {
		long diff = (to.getTime() - from.getTime())/1000l;
		if( diff == 0 ) {
			return "moments";
		}
		for( int i = 0; i < INTERVALS.length; i++ ) {
			diff /= INTERVAL_DIVISORS[ i ];
			if( i == INTERVAL_DIVISORS.length - 1 || diff < INTERVAL_DIVISORS[ i + 1 ]  ) {
				return String.valueOf( diff )+" "+INTERVALS[ i ]+(diff > 1 ? "s" : "");
			}
		}
		return "yonks";
	}
	
	public static String format( Date from ) {
		if( from == null )
			return "unknown";
		return format( from, new Date() )+" ago";
	}
	
}
